/*
 * Copyright 2010 Chad Retz
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.jdocng.parse.qdox.function;

import org.jdocng.parse.qdox.QDoxContext;
import org.jdocng.parse.qdox.QDoxUtils;
import org.jdocng.shared.model.SourcePositionImpl;
import org.jdocng.shared.model.TagImpl;

import com.thoughtworks.qdox.model.AbstractJavaEntity;
import com.thoughtworks.qdox.model.DocletTag;

/**
 * Construct {@link TagImpl} from {@link DocletTag}
 *
 * @author Chad Retz
 */
public class TagFunction<T extends TagImpl> implements ApplyingFunction<DocletTag, T> {
    
    private static final TagFunction<TagImpl> instance = new TagFunction<TagImpl>();
    
    public static TagFunction<? extends TagImpl> getTagFunction(DocletTag tag) {
        if ("@param".equals(tag.getName())) {
            return ParamTagFunction.getInstance();
        } else if ("@see".equals(tag.getName()) ||
                "@link".equals(tag.getName()) ||
                "@linkplain".equals(tag.getName())) {
            return SeeTagFunction.getInstance();
        } else if ("@serial".equals(tag.getName()) ||
                "@serialData".equals(tag.getName()) ||
                "@serialField".equals(tag.getName())) {
            return SerialTagFunction.getInstance();
        } else if ("@throws".equals(tag.getName()) ||
                "@exception".equals(tag.getName())) {
            return ThrowsTagFunction.getInstance();
        } else {
            return instance;
        }
    }
    
    protected static String getFirstWord(String contents) {
        int endFirstWordIndex = contents.indexOf(' ');
        if (endFirstWordIndex == -1) {
            return contents;
        } else {
            return contents.substring(0, endFirstWordIndex);
        }
    }
    
    protected TagFunction() {
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public T apply(QDoxContext context, DocletTag from) {
        TagImpl to = new TagImpl();
        apply(context, from, (T) to);
        return (T) to;
    }
    
    public void apply(QDoxContext context, DocletTag from, T to) {
        to.setFirstSentenceTags(QDoxUtils.getFirstSentenceTags(
                context, from));
        //TODO holder!
        //TODO inline tags!
        to.setName(from.getName());
        String file = null;
        if (from.getContext() instanceof AbstractJavaEntity) {
            file = ((AbstractJavaEntity) from.getContext()).
                    getSource().getURL().toExternalForm();
        }
        to.setPosition(new SourcePositionImpl(from.getLineNumber(),
                0, file));
        to.setText(from.getValue());
    }

}
